﻿' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2011 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace YahooManaged.Finance.Indicators


    Friend Class QuotesSorter
        Implements IComparer(Of KeyValuePair(Of Date, Double))

        Public Function Compare(ByVal x As KeyValuePair(Of Date, Double), ByVal y As KeyValuePair(Of Date, Double)) As Integer Implements System.Collections.Generic.IComparer(Of KeyValuePair(Of Date, Double)).Compare
            If x.Key > y.Key Then
                Return 1
            ElseIf x.Key < y.Key Then
                Return -1
            Else
                Return 0
            End If
        End Function
    End Class


    Friend Class HistQuotesSorter
        Implements IComparer(Of HistQuoteData)

        Public Function Compare(ByVal x As HistQuoteData, ByVal y As HistQuoteData) As Integer Implements System.Collections.Generic.IComparer(Of HistQuoteData).Compare
            If x.TradingDate > y.TradingDate Then
                Return 1
            ElseIf x.TradingDate < y.TradingDate Then
                Return -1
            Else
                Return 0
            End If
        End Function
    End Class



    Public Class QuotesConverter

        Public Function ConvertHistQuotesToSingleValues(ByVal quotes As IEnumerable(Of HistQuoteData), Optional ByVal adjusted As Boolean = False) As Dictionary(Of Date, Double)
            Dim dict As New Dictionary(Of Date, Double)
            If adjusted Then
                For Each hq As HistQuoteData In quotes
                    dict.Add(hq.TradingDate, hq.CloseAdjusted)
                Next
            Else
                For Each hq As HistQuoteData In quotes
                    dict.Add(hq.TradingDate, hq.Close)
                Next
            End If
            Return dict
        End Function


    End Class


End Namespace
